<?php

namespace app\admin\service;

use app\admin\model\BoxHotAvatar;
use think\Db;
use think\Exception;

class BoxHotAvatarService extends BaseService
{

    /**
     * 构造函数
     * LevelService constructor.
     */
    public function __construct()
    {
        $this->model = new BoxHotAvatar();
    }

    public  function getList()
    {
        $param = request()->param();
        // 查询条件
        $map = [];
        if (isset($param['status']) && !empty($param['status'])) {
            $map[] = ['status', '=', $param['status']];
        }
        if (isset($param['time'][0]) && !empty($param['time'][0])) {
            $map[] = ['create_time', '>=', strtotime($param['time'][0])];

        }
        if (isset($param['time'][1]) && !empty($param['time'][1])) {
            $map[] = ['create_time', '<=', strtotime($param['time'][1])];
        }
        $data= parent::getList($map); // TODO: Change the autogenerated stub
        if($data['count'] == 0) return $data;
        foreach ($data['data'] as $ke=>$item){
            $data['data'][$ke]['icon'] =imageUrl($item['icon']);
        }
        return $data;
    }

    /**
     * 默认头像
     * LevelService constructor.
     */
    public function default()
    {
        try{
            Db::startTrans();
            $param = request()->param();
            $arrIds = BoxHotAvatar::where(['is_default' =>1])->column('id');
            $this->model->where(['id' => $arrIds])->update(['is_default' =>0]);
            $this->model->where(['id' => $param['id']])->update(['is_default' =>$param['is_default'] ]);
            Db::commit();
            $this->model->cacheDelete($param['id']);
            if($arrIds){
                foreach ($arrIds as $val){
                    $this->model->cacheDelete($val);
                }
            }
            return message();
        }catch(Exception $e){
            Db::rollback();
            return message($e->getMessage(),false);
        }
    }
}